//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.restaurant.application.util;

import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
import java.nio.file.Path;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Hashtable;

public class ErCodeGen {
    public static final DateTimeFormatter sf = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    public static final int QRCODE_SIZE = 300;
    public static final String CONTENT = "";

    public ErCodeGen() {
    }

    public static void generateQRcodePic(String content, int width, int height, String picFormat) {
        Hashtable<EncodeHintType, Object> hints = new Hashtable();
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        hints.put(EncodeHintType.MARGIN, 1);

        try {
            BitMatrix bitMatrix = (new MultiFormatWriter()).encode(content, BarcodeFormat.QR_CODE, width, height, hints);
            String fileDir = "D:\\DEV_ENV" + File.separator + "image" + File.separator + LocalDate.now().format(sf);
            if (!(new File(fileDir)).exists()) {
                (new File(fileDir)).mkdirs();
            }

            Path file = (new File(fileDir + File.separator + "qrcode." + picFormat)).toPath();
            MatrixToImageWriter.writeToPath(bitMatrix, picFormat, file);
        } catch (IOException | WriterException var8) {
            var8.printStackTrace();
        }

    }

    public static byte[] generateQRcodeByte(String content, int width, String picFormat) {
        byte[] codeBytes = null;
        Hashtable<EncodeHintType, Object> hints = new Hashtable();
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
        hints.put(EncodeHintType.MARGIN, 0);

        try {
            BitMatrix bitMatrix = (new MultiFormatWriter()).encode(content, BarcodeFormat.QR_CODE, width, width, hints);
            BufferedImage image = toBufferedImage(bitMatrix);
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            ImageIO.write(image, picFormat, out);
            codeBytes = out.toByteArray();
        } catch (IOException | WriterException var8) {
            var8.printStackTrace();
        }

        return codeBytes;
    }

    public static BufferedImage toBufferedImage(BitMatrix matrix) {
        int width = matrix.getWidth();
        int height = matrix.getHeight();
        BufferedImage image = new BufferedImage(width, height, 1);
        int onColor = -16777216;
        int offColor = -1;

        for(int x = 0; x < width; ++x) {
            for(int y = 0; y < height; ++y) {
                image.setRGB(x, y, matrix.get(x, y) ? onColor : offColor);
            }
        }

        return image;
    }

    public static void readQRcode(String filepath) {
        MultiFormatReader multiFormatReader = new MultiFormatReader();
        File file = new File(filepath);
        BufferedImage image = null;
        BinaryBitmap binaryBitmap = null;
        Result result = null;

        try {
            image = ImageIO.read(file);
            binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
            result = multiFormatReader.decode(binaryBitmap);
        } catch (NotFoundException | IOException var7) {
            var7.printStackTrace();
        }

        System.out.println("读取二维码： " + result.toString());
        System.out.println("二维码格式： " + result.getBarcodeFormat());
        System.out.println("二维码内容： " + result.getText());
    }

    public static void main(String[] args) {
        generateQRcodePic("", 300, 300, "jpg");
        String filepath = "D:\\DEV_ENV" + File.separator + "image" + File.separator + LocalDate.now().format(sf) + File.separator + "qrcode.jpg";
        readQRcode(filepath);
        String path = "D:\\DEV_ENV" + File.separator + "image" + File.separator + LocalDate.now().format(sf);
        File pathDir = new File(path);
        if (!pathDir.exists()) {
            pathDir.mkdirs();
        }

        File pathFile = new File(path + File.separator + "qrcodeByte.jpg");
        byte[] fileIo = generateQRcodeByte("", 300, "jpg");

        try {
            OutputStream os = new FileOutputStream(pathFile);
            os.write(fileIo);
            os.flush();
            os.close();
        } catch (IOException var7) {
            var7.printStackTrace();
        }

    }
}
